home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / update~4.z / update~4 / lib_stdio_gets.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-06  |  1.3 KB  |  51 lines

  1. /*                g e t s
  2.  *
  3.  * Read a line from stdin. The line is read up until the next
  4.  * newline character or EOF is encountered. The newline
  5.  * character is not inserted into the buffer, but the buffer
  6.  * is terminated with a null character. No checks are made
  7.  * that the line will fit into the buffer. The function returns
  8.  * a pointer to the buffer. If EOF is encountered before any
  9.  * characters have been read, the NULL pointer is returned.
  10.  *
  11.  * Patchlevel 1.0
  12.  *
  13.  * Edit History:
  14.  * 02-Sep-1989    Speed up by reading directly from buffer.
  15.  */
  16.  
  17. #include "stdiolib.h"
  18.  
  19. /*LINTLIBRARY*/
  20.  
  21. char *gets(buf)
  22.  
  23. char *buf;                /* input buffer */
  24.  
  25. {
  26.   int ch;                /* next character */
  27.   unsigned char *q;            /* input buffer pointer */
  28.   unsigned char *s;            /* user's buffer pointer */
  29.   unsigned int bytesleft;        /* bytes left in current load */
  30.  
  31.   if (TESTFLAG(stdout, _IOLBF))
  32.     (void) fflush(stdout);
  33.  
  34.   for (s = (unsigned char *) buf; ;*s++ = ch) {
  35.     if ((bytesleft = BYTESINREADBUFFER(stdin)) != 0) {
  36.       q = GETREADPTR(stdin);
  37.       UNROLL_DO(fgetsbytes, bytesleft, if ((*s++ = *q++) == '\n') break);
  38.       SETREADPTR(stdin, q);
  39.     }
  40.     if (bytesleft != 0) {
  41.       *--s = 0;
  42.       return buf;
  43.     }
  44.     *s = 0;
  45.     if ((ch = getc(stdin)) == EOF)
  46.       return s == (unsigned char *) buf ? NULL : buf;
  47.     if (ch == '\n')
  48.       return buf;
  49.   }
  50. }
  51.